package com.quanquan.management.controller;

import base.BaseController;
import com.quanquan.management.entity.SysRole;
import com.quanquan.management.entity.SysRoleUser;
import com.quanquan.management.entity.SysUser;
import com.quanquan.management.service.SysRoleService;
import com.quanquan.management.service.SysRoleUserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;

/**
 * @author gongzhou
 * @title: ManagerBaseController
 * @projectName SpringCloud
 * @description: TODO
 * @date 2019/3/289:17
 */
public class ManagerBaseController extends BaseController {

    @Autowired
    private SysRoleUserService sysRoleUserService;
    @Autowired
    private SysRoleService sysRoleService;
    @Value("${module-url.ipDomain}")
    private String ipDomain;

    @ModelAttribute
    public void setModelAttr(Model model) {
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        if (null == user) {
            return;
        }
        SysRoleUser byUserId = sysRoleUserService.findByUserId(user.getId());
        if (null != byUserId) {
            SysRole one = sysRoleService.getOne(byUserId.getRoleId());
            model.addAttribute("sysRole",one);
        }
        model.addAttribute("sysUser", user);
        model.addAttribute("ipDomain", ipDomain);
    }

}
